//
//  TransferOutCenterView.m
//  Wallet
//
//  Created by  ztryxh on 2018/6/12.
//  Copyright © 2018年 ebo. All rights reserved.
//

#import "TransferOutCenterView.h"
#import "UIButton+Layout.h"
#import "AddressModel.h"
#import "CoinDetailModel.h"
#import "NSDecimalNumber+Addtion.h"
@interface TransferOutCenterView()<UITextViewDelegate>
{
    UIView *bottomLine;
    UILabel *topLeftLabel;
    UILabel *bottomLeftLabel;
//    TDTitleBtn *alwaysBtn;
    UIButton *scanBtn;
    UILabel *coinNameLabel;
    NSString *cnyPrice;
    UILabel *placeHolderLabel;
}
@property (nonatomic,strong) UIView *addressBackView;
@property (nonatomic,strong) UIView *nunBackView;
@property (nonatomic,strong) UITextView *addressTextView;
@property (nonatomic,strong) UIButton  *getNewAddressBtn;
@property (nonatomic,strong) UILabel  *feeNumLabel;
@property (nonatomic,strong) UILabel  *cnyLabel;
@end
@implementation TransferOutCenterView
#pragma mark - privacy
-(instancetype)init{
    
    if(self==[super init]){
        
        [self initUI];
    }
    return self;
}
-(void)layoutSubviews{
    
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.left.right.equalTo(self);
        make.height.mas_equalTo(1);
    }];
    
    [topLeftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self).offset(kSCALE_WIDTH(12));
        make.top.equalTo(self).offset(kSCALE_WIDTH(15));
        
    }];
    
    [self.addressBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(topLeftLabel);
        make.right.equalTo(self.mas_right).offset(-kSCALE_WIDTH(12));
        make.top.equalTo(topLeftLabel.mas_bottom).offset(kSCALE_WIDTH(12));
        make.height.mas_equalTo(kSCALE_WIDTH(63));
    }];
    [bottomLeftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(topLeftLabel.mas_left);
        make.top.equalTo(self.addressBackView.mas_bottom).offset(kSCALE_WIDTH(21));
    }];
    
    [self.nunBackView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.equalTo(self.addressBackView);
        make.height.mas_equalTo(kSCALE_WIDTH(40));
        make.top.equalTo(bottomLeftLabel.mas_bottom).offset(kSCALE_WIDTH(12));
    }];
    [self.feeNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.equalTo(self.nunBackView);
        make.top.equalTo(self.nunBackView.mas_bottom).offset(kSCALE_WIDTH(11));
    }];
    [self.cnyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.nunBackView);
        make.centerY.equalTo(self.feeNumLabel);
    }];
//    [alwaysBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.right.equalTo(self.addressBackView);
//        make.centerY.equalTo(topLeftLabel);
//    }];
    [scanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.addressBackView).offset(-kSCALE_WIDTH(8));
        make.centerY.equalTo(self.addressBackView);
    }];
    
    [self.addressTextView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(self.addressBackView).insets(UIEdgeInsetsMake(kSCALE_WIDTH(8), kSCALE_WIDTH(6), kSCALE_WIDTH(8), kSCALE_WIDTH(75)));
    }];
    [placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.addressTextView).offset(3);
        make.top.equalTo(self.addressTextView).offset(8);
    }];
    
    [coinNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.equalTo(self.nunBackView).offset(-kSCALE_WIDTH(8));
        make.centerY.equalTo(self.nunBackView);
    }];
    
    [self.numTextField mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.equalTo(self.nunBackView);
        make.left.equalTo(self.nunBackView).offset(kSCALE_WIDTH(11));
    }];
}
-(void)initUI{
    
    placeHolderLabel=[self privacy_createLabel];
    [placeHolderLabel setTextColor:kGrayColor];
//    [placeHolderLabel setText:[NSString stringWithFormat:@"请输入%@转账地址",self.model.type]];
    
//    [self setBackgroundColor:kBackColor];
    bottomLine=[[UIView alloc]init];
//    [bottomLine setBackgroundColor:kSetColor(@"1e2235")];
    [self addSubview:bottomLine];
    
    topLeftLabel=[self privacy_createLabel];
    [topLeftLabel setText:@"转账地址"];
    [self addSubview:topLeftLabel];
    
    [self addSubview:self.addressBackView];
    
    
    bottomLeftLabel=[self privacy_createLabel];
    [bottomLeftLabel setText:@"转账金额"];
    [self addSubview:bottomLeftLabel];
    
    
    [self addSubview:self.nunBackView];
    [self addSubview:self.feeNumLabel];
    [self addSubview:self.cnyLabel];
    [self.feeNumLabel setText:@"手续费"];
    
    

    
    scanBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [scanBtn setImage:[UIImage imageNamed:@"icon_qr"] forState:UIControlStateNormal];
    [scanBtn addTarget:self action:@selector(scanBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.addressBackView addSubview:scanBtn];
    
    [self.addressBackView addSubview:self.addressTextView];
    [self.addressBackView addSubview:placeHolderLabel];
    [self.nunBackView addSubview:self.numTextField];
    
    coinNameLabel=[self privacy_createLabel];
    [coinNameLabel setTextColor:kGrayColor];
    [coinNameLabel setText:@""];
    [self.nunBackView addSubview:coinNameLabel];
    
    __weak typeof (UITextField*)weakNumField = _numTextField;
    [[_numTextField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
        
//        if ([WJTools isBlankString:weakNumField.text]) {
//            weakNumField.text = self.model.zc_min;
//        }
        if (self.getAmountBlock) {
            self.getAmountBlock(weakNumField.text);
        }
    }];
    
}
-(UILabel *)privacy_createLabel
{
    UILabel *label=[[UILabel alloc]init];
    [label setTextColor:[UIColor blackColor]];
    [label setFont:FONT(14)];
    return label;
}

-(void)scanBtnClick{
    
    if(self.addAddressBlock){
        
        self.addAddressBlock();
    }
}
-(void)textViewDidChange{
    
    [self.cnyLabel setText:[NSString stringWithFormat:@"约¥%.2f",cnyPrice.floatValue*self.numTextField.text.floatValue]];
    
    
}
-(void)textViewDidChange:(UITextView *)textView{
    
    if(textView.text.length==0){
        [placeHolderLabel setHidden:NO];
    }
    else{
        [placeHolderLabel setHidden:YES];
        
    }
}
//-(void)alwaysBtnClick{
//    if(self.selectAddressBlock){
//
//        self.selectAddressBlock(alwaysBtn);
//    }
//}
#pragma mark - lazy

-(UILabel *)feeNumLabel{
    
    if(!_feeNumLabel){
        
        _feeNumLabel=[self privacy_createLabel];
        [_feeNumLabel setFont:FONT(12)];
        [_feeNumLabel setTextColor:[UIColor blackColor]];
        [_feeNumLabel setTextAlignment:NSTextAlignmentRight];
    }
    return _feeNumLabel;
}
-(UILabel *)cnyLabel{
    
    if(!_cnyLabel){
        
        _cnyLabel=[self privacy_createLabel];
        [_cnyLabel setFont:FONT(12)];
        [_cnyLabel setTextColor:[UIColor blackColor]];
        [_cnyLabel setTextAlignment:NSTextAlignmentLeft];
    }
    return _cnyLabel;
}
-(UIView*)addressBackView{
    
    if(!_addressBackView){
        
        _addressBackView=[[UIView alloc]init];
//        [_addressBackView setBackgroundColor:kSetColor(@"1e2235")];
        [_addressBackView.layer setBorderColor:kGetColor(58, 64, 90).CGColor];
        [_addressBackView.layer setBorderWidth:kOnePointHeight];
        [_addressBackView.layer setCornerRadius:2];
        
        
    }
    return _addressBackView;
}
-(UIView *)nunBackView{
    
    if(!_nunBackView){
        
        _nunBackView=[[UIView alloc]init];
//        [_nunBackView setBackgroundColor:kSetColor(@"1e2235")];
        [_nunBackView.layer setBorderColor:kGetColor(58, 64, 90).CGColor];
        [_nunBackView.layer setBorderWidth:kOnePointHeight];
        [_nunBackView.layer setCornerRadius:2];
    }
    return _nunBackView;
}
-(UITextView *)addressTextView{
    
    if(!_addressTextView){
        
        _addressTextView=[[UITextView alloc]init];
        [_addressTextView setFont:FONT(14)];
        [_addressTextView setTextColor:kGrayColor];
        [_addressTextView setScrollEnabled:NO];
        _addressTextView.delegate = self;
//        [_addressTextView setBackgroundColor:[UIColor clearColor]];
        [_addressTextView setText:@""];
    
    
    }
    return _addressTextView;
}
-(UITextField *)numTextField{
    
    if(!_numTextField){
        
        _numTextField =[[UITextField alloc]init];
        [_numTextField setFont:FONT(14)];
        [_numTextField setTextColor:kGrayColor];
        [_numTextField setAttributedPlaceholder:[self setMyPlaceHolder:@"最小提币数量---"]];
        [_numTextField setDelegate:self];
        [_numTextField setKeyboardType:UIKeyboardTypeDecimalPad];
        [_numTextField addTarget:self action:@selector(textViewDidChange) forControlEvents:UIControlEventEditingChanged];
    
    }
    return _numTextField;
}
-(void)setModel:(TransferOutInfoModel *)model{
    _model = model;
    
    [self.addressTextView setText:model.addressModel.address];
    if(![model.amount isEqualToString:@"0"]){
        [self.numTextField setText:model.amount];
    }
   

    NSString *proportion = [NSString stringWithFormat:@"%@", SNMul(model.zc_fee_proportion,model.amount)];
    NSString *tax = [NSString stringWithFormat:@"%@", SNMax(proportion, model.zc_min_fee ).stringValue] ;
    [self.feeNumLabel setText:[NSString stringWithFormat:@"手续费%@ %@",tax,model.type]];
    cnyPrice=model.cny_price;
    [coinNameLabel setText:model.type];
     [_numTextField setAttributedPlaceholder:[self setMyPlaceHolder:[NSString stringWithFormat:@"最小提币数量%@",model.zc_min]]];
     [placeHolderLabel setText:[NSString stringWithFormat:@"请输入%@转账地址",self.model.type]];
    
    
}
//设置uitextfield的placeHolder的颜色为白色
-(NSMutableAttributedString *)setMyPlaceHolder:(NSString *)str{
    
    NSMutableAttributedString* placeHolderStr = [[NSMutableAttributedString alloc] initWithString:str];
    [placeHolderStr addAttribute:NSUnderlineStyleAttributeName
                           value:@(NSUnderlineStyleNone)
                           range:(NSRange){0,[placeHolderStr length]}];
    [placeHolderStr addAttribute:NSForegroundColorAttributeName value:kGrayColor  range:NSMakeRange(0,[placeHolderStr length])];
    return placeHolderStr;
    
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if (self.addressChangeBlock) {
        self.addressChangeBlock(textView.text);
    }
}
@end
